const config = require('./config')

const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const router = require('./routers/index')
const errorHandler = require('./middleware/errorHandler')
const app = express()

// 处理json
app.use(express.json())
app.use(express.urlencoded({extended:false}))
// 处理跨域
app.use(cors())
// 处理日志
app.use(morgan('dev'))
// 静态资源托管
app.use(express.static('public'))

// 引入数据库
require('./model')


// 引入路由
app.use('/api',router)
app.get('/', (req, res) => {
    res.send('OK')
})

// 错误中间件一定要放在最后面
app.use(errorHandler)

app.listen(config.app.port, () => {
    console.log(`Running at http://localhost:${config.app.port}`);
})